#!/usr/bin/env python3
"""
生成 Chrome 扩展图标
需要安装 Pillow: pip install Pillow
"""

import os
from pathlib import Path

try:
    from PIL import Image, ImageDraw
except ImportError:
    print("请先安装 Pillow: pip install Pillow")
    exit(1)

def create_icon(size):
    """创建指定尺寸的图标"""
    # 创建图像
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 微信绿色背景（圆角矩形效果用椭圆近似）
    bg_color = (7, 193, 96, 255)  # #07c160
    
    # 绘制圆角矩形背景
    radius = size // 5
    draw.rounded_rectangle([0, 0, size-1, size-1], radius=radius, fill=bg_color)
    
    # 绘制白色文档图标
    doc_color = (255, 255, 255, 255)
    margin = size // 6
    doc_left = margin + size // 10
    doc_top = margin
    doc_right = size - margin
    doc_bottom = size - margin
    
    # 文档主体
    draw.rectangle([doc_left, doc_top + size//10, doc_right, doc_bottom], fill=doc_color)
    
    # 折角效果
    corner_size = size // 6
    fold_color = (212, 245, 226, 255)  # 浅绿色
    points = [
        (doc_right - corner_size, doc_top),
        (doc_right, doc_top + corner_size),
        (doc_right - corner_size, doc_top + corner_size)
    ]
    draw.polygon(points, fill=fold_color)
    
    # 顶部斜边
    draw.polygon([
        (doc_left, doc_top + size//10),
        (doc_right - corner_size, doc_top),
        (doc_right - corner_size, doc_top + size//10)
    ], fill=doc_color)
    
    # 添加横线（模拟文字）
    if size >= 48:
        line_color = (7, 193, 96, 255)
        line_height = max(1, size // 30)
        line_width = (doc_right - doc_left) * 2 // 3
        line_x = doc_left + (doc_right - doc_left - line_width) // 2
        line_spacing = size // 10
        
        for i in range(3):
            line_y = doc_top + size // 3 + i * line_spacing
            if line_y + line_height < doc_bottom - margin // 2:
                draw.rectangle(
                    [line_x, line_y, line_x + line_width, line_y + line_height],
                    fill=line_color
                )
    
    return img

def main():
    # 获取脚本所在目录的父目录（项目根目录）
    script_dir = Path(__file__).parent
    project_dir = script_dir.parent
    icons_dir = project_dir / 'icons'
    
    # 确保 icons 目录存在
    icons_dir.mkdir(exist_ok=True)
    
    # 生成三个尺寸的图标
    sizes = [16, 48, 128]
    
    for size in sizes:
        icon = create_icon(size)
        filepath = icons_dir / f'icon{size}.png'
        icon.save(filepath, 'PNG')
        print(f'✓ 已生成: {filepath}')
    
    print(f'\n图标已保存到 {icons_dir} 目录')

if __name__ == '__main__':
    main()

